<template>
	<view class="box">
		<view v-if="this.itme.id==0">
			<u-empty       text="未查询到订单详情"    icon="https://cdn.uviewui.com/uview/empty/history.png"></u-empty>
		</view>
		<view v-else>
			<watermark-camera :imgLists="imgList" :imgNumber="3" @clickTakePhoto="clickTakePhoto"
			 :userName="itme.userName"
			 :serviceName="itme.serveName"  @takePhotoSubmit="takePhotoSubmit"></watermark-camera>
		</view>
		
	</view>
</template>

<script>
	import watermarkCamera from "@/components/watermark-camera/watermark-camera";
	export default {
		components: {
		    watermarkCamera
		},
		data() {
			return {
				imgList:[],
				fileList:[],
				itme:{},
			}
		},
		onLoad(options) {
			console.log("sss："+JSON.stringify(options))
			if ('params' in options) {
			    let data = JSON.parse(decodeURIComponent(options.params));
			    console.log("服务中拍照",data)
				this.itme=data
				this.imgList=JSON.parse(data.workingAchievementPicUrl)
			}else{
				this.itme.id=0
			}
		},
		methods: {
			//点击上传
			takePhotoSubmit(url){
				console.log('点击上传：' +JSON.stringify(url));
				this.fileList1=url
				this.updateStatus('inService')
			},
			//点击拍照返回
			 clickTakePhoto(url,urlList){
				console.log('点击拍照返回：' +url.imgList);
			},
			//开始服务 H2025030500002 639
			updateStatus(type){
				
				if(this.fileList1.length==0){
					uni.$u.toast("请上传门头照片")
					return
				}
				 let data={
					workingAchievementPicUrl:JSON.stringify(this.fileList1),
					id:this.itme.orderId,//订单id
					technicianId:this.itme.technicianId,//技师id
					dispatchId:this.itme.id,//派单id
					appointmentTime:this.itme.appointmentTime,
				}
				console.log("提交开始服务data",data)
				this.request.postRequest(this.http.httpsLian.homemakingAppOrderUpdateFUZ,data, true)
				.then(res => { 
					console.log('开始服务返回',res);
					let iconMsg;
					if(res.code==0){
						uni.showToast({
							title:'上传成功',
							icon:"success",
							duration: 1500
						})
						 setTimeout(()=>{
						 	uni.navigateBack({
						 		delta:1
						 	})
						 },1500)
						
					}else{
						uni.showToast({
							title:'上传失败',
							icon:"error",
							duration: 1500
						})
					}
					
					
				 }).catch(res => {
					uni.$u.toast("网络异常")
				})
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url,event.name)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			//图片上传    
			uploadFilePromise(url,name) {
				let _this=this
				let token = uni.getStorageSync("token");
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						//url: this.$common.domain+'/api/common/upload', // 仅为示例，非真实的接口地址
						url:this.http.httpsLian.upload, // 仅为示例，非真实的接口地址
						filePath: url,
						header: {
							'tenant-id': 1,
							//"content-type": "application/json",//"Content-Type": "multipart/form-data",
							"Authorization": "Bearer "+token
						},
						name: 'file',
						success: (res) => {
							let data =JSON.parse(res.data)
							if(data.code!=0){
								setTimeout(() => {
								const num = _this[`fileList${name}`].findIndex(v => v.url === url);
									_this[`fileList${name}`].splice(num, 1);
								}, 500)
								uni.$u.toast("上传失败")
								return
							}
							//uni.$u.toast("上传成功")
							
							resolve(data.data)
							
						}
					});
				})
			},    
			
			//唤醒手机拍照  
			chooseImage(){
				uni.chooseImage({
					count: 6, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'],   //album 从相册选图，camera 使用相机
					success: function (res) {
						console.log(JSON.stringify(res.tempFilePaths)); //拍照图片的路径
						this.fileList3.push({
							url:res.tempFilePaths
						})
					}
				});
			},
			pickerClose(){
				this.pickerShow=false;
			}
		}
	}
</script>

<style lang="scss" scoped>
.box{
	margin-top: 10rpx;
	padding: 0px 10px;
	//background-color: white;
}	
.fontweight{
	font-weight: 550;
}
.content1{
	margin-top: 20px;
	/deep/.u-upload__button{
		background-color: white;
		
	}
	view{
		margin-top: 10px;
	}
}
span{
	font-size: 11px;
	color: $uni-text-color-grey;
}
p{
	font-size: 12px;
}
.bottom{
	background: white;
	padding: 10px;
	
	/deep/.u-input{
		width: 120px !important;
		//background-color: rgb(245, 247, 250);
	}
	
	.fontweight{
		margin-bottom: 20px;
	}
}
/deep/.u-button{
	    color: black !important;
		margin-top: 20px;
		margin-bottom: 20px;
	}
	.reservation{
		background-color: white;
		padding: 10px 0px;
	}
</style>
